package com.tarena.lbs.coupon.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.dao.mapper.CouponMapper;
import com.tarena.lbs.coupon.dao.repository.CouponRepository;
import com.tarena.lbs.coupon.base.enums.CouponStatusEnum;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.po.CouponPO;
import com.tarena.lbs.coupon.pojo.po.CouponStockPO;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZHC
 */
@Repository
@Slf4j
public class CouponRepositoryImpl implements CouponRepository {
    @Autowired
    private CouponMapper couponMapper;

    @Override
    public Integer instertCoupon(CouponParam couponParam) {
        log.info("Coupon数据层:新增优惠券:{}",couponParam);
        // 新增优惠券基础数据
        CouponPO couponPO = new CouponPO();
        BeanUtils.copyProperties(couponParam,couponPO);
        couponPO.setStatus(CouponStatusEnum.STATUS_ACTIVATE.getCode());
        Date date = new Date();
        couponPO.setUpdateAt(date);
        couponPO.setCreateAt(date);
        couponPO.setEnableStatus(CouponStatusEnum.ENABLE_STATUS_UNUSED.getCode());
        couponMapper.insert(couponPO);
        return couponPO.getId();
    }

    @Override
    public PageResult<CouponVO> selectPageList(Integer pageNum, Integer pageSize, Integer businessId) {
        log.info("Coupon数据层:查询优惠券分页列表:{},{}",pageNum,pageSize);
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        // 创建分页对象
        Page<CouponPO> page = new Page<>(pageNum,pageSize);
        // 创建查询条件
        // sql : select * from coupon where business_id = ?
        QueryWrapper<CouponPO> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("business_id",businessId);
        // 执行分页查询
        IPage<CouponPO> result = couponMapper.selectPage(page, queryWrapper);
        log.info("Coupon数据层:查询优惠券分页列表:{}",result);
        // 转换为业务层需要的格式
        PageResult<CouponVO> pageResult = new PageResult<>(pageNum, pageSize);
        pageResult.setTotal(result.getTotal());
        List<CouponVO> voList = result.getRecords().stream()
                .map(po -> {
                    CouponVO vo = new CouponVO();
                    BeanUtils.copyProperties(po, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        pageResult.setObjects(voList);
        return pageResult;
    }

    @Override
    public CouponVO selectById(Integer id) {
        log.info("Coupon数据层:查询优惠券详情:{}",id);
        // 查询sql:select * from coupon where id = ?
        CouponPO couponPO = couponMapper.selectById(id);
//        log.info("Coupon数据层:查询优惠券详情:couponPO:{}",couponPO);
        CouponVO couponVO = new CouponVO();
        BeanUtils.copyProperties(couponPO,couponVO);
//        log.info("Coupon数据层:查询优惠券详情:couponVO:{}",couponVO);
        return couponVO;
    }

    @Override
    public void updateCoupon(CouponParam couponParam) {
        CouponPO couponPO = new CouponPO();
        BeanUtils.copyProperties(couponParam,couponPO);
        couponMapper.updateById((couponPO));
    }

}
